#include "words.h"
#include "allocate.h"

/** @fn     char charsimplify(char x)
    @brief	convert all incoming uppercase characters to lowercase
	@param	x   the character to be changed
	@return	case-changed char
    */
char charSimplify(char x){
    //if(DEBUG){printf("charSimplify(%c)", x);}

    //if(!x){if(DEBUG){printf("empty char");}return(0);}
    //check chars within certain range

	if((x >= 0x41 ) && (x <= 0x5A)){
		//if(DEBUG){printf("\ncharacter:\t\tCase-change %c (0x%X) to %c (0x%X)", x, x, x+32, x+32);}
		return(x+32);
	}else{
		return(x);
	}
}

/** @fn     char* wordbuild(FILE* fTarget)
    @param  fTarget file pointer of source file
    @brief  searches a file and seperates it into words (delimiter are spaces, line carriages and EOF bytes)
	@return returns pointer to word in allocated memory space
    */
char* wordBuild(FILE* fTarget){

    //exit func upon empty file pointer
    if(fTarget == NULL){
        if(DEBUG){printf("\nno file given to wordBuild()");}
        return(NULL);
    }

    //declarations
    char* pWord = NULL;
    char word[MAX_WORD_SIZE] = "";
	int curpos = 0;
	int lastchar;
	int p;

    if(DEBUG>1){printf("\n\nreading word");}
    while(curpos < MAX_WORD_SIZE){
        p = (int) NULL; lastchar = (int) NULL;
        if(DEBUG>1){printf(".");}
        //if valid file ptr
        if(fTarget){
            if(DEBUG>1){printf(":");}
            //if read succeeded
            if((p = fgetc(fTarget))){
                if(DEBUG>1){printf(".");}
                //if char read from file
                if(p != (int) EOF){
                    lastchar = charSimplify(p);
                    //deal with alfabetic characters
                    if(
                        ((lastchar > 64) && (lastchar < 91 )) ||
                        ((lastchar > 96) && (lastchar < 123 )))
                        {
                            word[curpos] = (char) lastchar;
                            curpos++;
                    }else{
                        if(DEBUG>1){printf("...done");}
                        break;
                    }
                }else{
                    boolEOF = 1;
                    if(DEBUG>1){printf("...EOF");}
                    break;
                }
            }else{
                //if read (fgetc) failed
                if(DEBUG>1){printf("...fail\nread error");}
            }
        }else{
            //deal with empty file pointer (fTarget)
            if(DEBUG>1){printf("...fail\nempty file pointer");}
            boolEOF = 1;
            break;
        }
    }

    //replace "delimiter" char w/ terminating char
    if(curpos >= MAX_WORD_SIZE){curpos = MAX_WORD_SIZE-1;}

    word[curpos] = '\0';
    if(curpos <= 1){if(DEBUG>2){printf("\nempty string");};return(NULL);}

    char string[curpos+1];
    strcpy(string, word);

    if((pWord = malloc(sizeof(string))) != NULL){
        if(DEBUG>3){printf("\nword \"%s\" @ 0x%X", string, (int) pWord);}
        for(p=0; p < (strlen(string) + 1); p++){
            //printf("\n\tstrlen:\t%d\n\tindex:\t%d %c", strlen(string), p, string[p]);
            *(pWord + p) = string[p];
        }
        return(pWord);
    }else{
        if(DEBUG>1){printf("\nmalloc operation failed");}
        return(NULL);
    }

}

/** @fn     int wordCompare(char *string1, int len1, char *string2, int len2){
    @param  string1 pointer to first string
    @param  len1    length of string1
    @param  string2 pointer to second string
    @param  len2    length of string2
    @brief  compares two words on alfabetical rank
	@return return 1 for decreasing inalphabetic order, 2 for increasing alphabetical order
    */
int wordCompare(char *string1, char *string2){
    if((string1 == (char *) NULL) || (string2 == (char *) NULL)){if(DEBUG>2){printf("\ninvalid string pointers for compare op");}return(0);}
    if(DEBUG>4){printf("\ncompare 0x%X to 0x%X", (int) string1, (int) string2);}
    return(strcmp(string1, string2));
}

void wordbreak(char* pWord){
    free(pWord);
    if(DEBUG == 2){printf("\nDESTROY");}
}
